using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using Ltf.Remoting.TeamFoundation;
using System.IO;
using Ltf.MonitorLizard.TFAccess;
using System.Windows.Forms;
//blah
namespace Ltf.MonitorLizard.Helpers
{
	class LizardFileAccess
	{
		public static string GetFilePath(string path)
		{
			return GetFilePath(path, string.Empty);
		}

		public static string GetFilePath(string path, String resultSuffix)
		{
			string[] pv = path.Split(new string[] { ";c" }, StringSplitOptions.RemoveEmptyEntries);
			if (pv.Length == 2)
			{
				string filePath = pv[0];
				int v = 0;
				int.TryParse(pv[1], out v);
				return GetFilePath(filePath, resultSuffix, v.ToString());
			}
			else
			{
				return GetFilePath(path, resultSuffix, "0");
			}
		}
		public static string GetFilePath(string path, string resultSuffix, string version)
		{
			if (path.StartsWith("$"))
			{
				VersionControlServer vcServer = ConnectionInstance.Connection.VersionControlServer;
				TFAccess.TFDifRepository repo = (TFAccess.TFDifRepository)GetRepositoryForPath(path, version);
				if (repo.FileExists(path))
				{
					string tempFile = ConnectionInstance.Connection.Settings.TempPath + "\\"
						+ Path.GetFileName(path) + "." + repo.Version + "." + resultSuffix;
					using (Stream s = repo.GetFileItemStream(path))
					{
						byte[] buffer = new byte[255];
						using (FileStream fs = File.OpenWrite(tempFile))
						{
							int i;
							while ((i = s.Read(buffer, 0, 255)) > 0)
							{
								fs.Write(buffer, 0, i);
							}
						}
					}
					return tempFile;
				}
				throw new System.IO.FileNotFoundException();
			}
			else
				return path;
		}

		public static string GetLocalTFVersionFilePath(string path)
		{
			TFAccess.LocalTFDifRepository localTf = new LocalTFDifRepository
			(ConnectionInstance.Connection.VersionControlServer, ConnectionInstance.Connection.Settings.TempPath);
			if (localTf.FileExists(path))
			{
				string tempFile = ConnectionInstance.Connection.Settings.TempPath + "\\"
					+ Path.GetFileName(path) + ".tflocalversion";
				using (Stream s = localTf.GetFileItemStream(path))
				{
					byte[] buffer = new byte[255];
					using (FileStream fs = File.OpenWrite(tempFile))
					{
						int i;
						while ((i = s.Read(buffer, 0, 255)) > 0)
						{
							fs.Write(buffer, 0, i);
						}
					}
				}
				return tempFile;
			}
			throw new System.IO.FileNotFoundException();

		}

		public static IDifRepository GetRepositoryForPath(string path, string versionText)
		{
			if (path.StartsWith("$"))
			{
				VersionControlServer vcServer = ConnectionInstance.Connection.VersionControlServer;
				if (versionText == "local")
				{
					TFAccess.LocalTFDifRepository localRepo = new LocalTFDifRepository(vcServer, ConnectionInstance.Connection.Settings.TempPath);
					return localRepo;
				}
				TFAccess.TFDifRepository tfRepo = new TFAccess.TFDifRepository(vcServer, ConnectionInstance.Connection.Settings.TempPath);
				int version = 0;
				if (int.TryParse(versionText, out version))
				{
					tfRepo.Version = version;
				}

				return tfRepo;
			}
			else
			{
				return new FSDifRepository();
			}
		}

		public static string ReversionPath(string path, int newVersion)
		{
			string name;
			int v;
			GetNameAndVersionFromPath(path, out name, out v);

			return string.Format("{0};c{1}", name, newVersion.ToString());
		}

		public static void LoadVersions(ComboBox cmbVersion, string path)
		{
			int v;
			string name;
			GetNameAndVersionFromPath(path, out name, out v);
			LoadVersions(cmbVersion, name, v);
		}

		public static void LoadVersions(ComboBox cmbVersion, string path, int currentVersion)
		{
			try
			{
				cmbVersion.Items.Clear();
				cmbVersion.Items.Add("Latest");
				cmbVersion.SelectedIndex = 0;
				VersionControlServer vcServer = ConnectionInstance.Connection.VersionControlServer;
				int latestChangesetId = vcServer.GetLatestChangesetId();
				object history = vcServer.QueryHistory(path, new ChangesetVersionSpec(latestChangesetId), 0, RecursionType.Full, "",
					new ChangesetVersionSpec(1), new ChangesetVersionSpec(latestChangesetId), 200, true, false);
				System.Collections.IEnumerable changesets = (System.Collections.IEnumerable)history;

				foreach (Changeset c in changesets)
				{
					cmbVersion.Items.Add(c.ChangesetId);
					if (currentVersion == c.ChangesetId)
						cmbVersion.SelectedIndex = cmbVersion.Items.Count - 1;
				}
			}
			catch
			{

			}
		}

		public static void GetNameAndVersionFromPath(string path,out string name, out int version)
		{
			int v = 0;
			name = path;
			string[] pv = path.Split(new string[] { ";c" }, StringSplitOptions.RemoveEmptyEntries);
			if (pv.Length == 2)
			{
				name = pv[0];
				if (!int.TryParse(pv[1],out v))
				{
					if (pv[1] == "local")
						v = -1;
				}
			}
			version = v;
		}

	}
}
